<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <button @click='$router.push("/home")'>首页</button>
        <button @click='$router.push("/news")'>新闻</button>
        <button @click='$router.push("/me")'>我的</button>
        <router-view></router-view>
    </div>
</body>
<script src="./vue.js"></script>
<script src="./vue-router.js"></script>
<script>
    
    const originalPush = VueRouter.prototype.push
    VueRouter.prototype.push = function push(location) {
      return originalPush.call(this, location).catch(err => err)
    }
    const home={
        template:`
            <div>
                <span>我是home页</span>
                <button @click='$router.push("/home/child1")'>去chil1</button>
                <button @click='$router.push("/home/child2")'>去chil2</button>
                <router-view></router-view>
            </div>
        
        `
    };
    const news={
        template:`
            <div>
                <span>我是news页</span>
            </div>
        
        `
    };
    const me={
        template:`
            <div>
                <span>我是me页</span>
            </div>
        
        `
    };
    const child1={
        template:`
            <div>
                <span>child1</span>
            </div>
        
        `
    };
    const child2={
        template:`
            <div>
                <span>child2</span>
            </div>
        
        `
    }
    const routes=[
        {
            path:'/',
            redirect:'/home'

        },
        {
            path:'/home',
            name:'首页',
            component:home,
            children:[
                {
                    path:'/home',
                    redirect:'/home/child1'
                },
                {
                    path:'/home/child1',
                    component:child1
                },
                {
                    path:'/home/child2',
                    component:child2
                }
            ]
        },
        {
            path:'/news',
            name:'新闻',
            component:news
        },
        {
            path:'/me',
            name:'我的',
            component:me
        }
    ]
    const router=new VueRouter({
        routes
    })
    const vm=new Vue({
        el:'#app',
        router
    })
</script>
</html>